//
//  ZYMyStoreViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/30.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYMyStoreViewController.h"

#import "ZYChangeOilsPriceViewController.h"

#import "ZYMyStoreOilsPriceTableViewCell.h"

#import "LBSingleImagePicker.h"

#import "ZYMyStoreModel.h"

#import "OSSImageUploader.h"

@interface ZYMyStoreViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *headerView;


@property (weak, nonatomic) IBOutlet UILabel *storeNameLabel;


/// 门店位置
@property (weak, nonatomic) IBOutlet UILabel *locationLabel;

@property (weak, nonatomic) IBOutlet UIButton *storeImageButton;

@property (nonatomic, strong) IBOutlet UITableView *tableView;

@property (nonatomic, strong) LBSingleImagePicker *picker;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;

@property (nonatomic, strong) ZYMyStoreModel *storeModel;

@end

@implementation ZYMyStoreViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configViews];
    [self configData];
    
    self.topLayout.constant = -(20 + IPX_STATUSBAROFFSETHEIGHT);
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

#pragma mark - Setup View / Data

- (void)configViews {
    self.title = @"我的门店";
    
    self.navigationController.navigationBarHidden = YES;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYMyStoreOilsPriceTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZYMyStoreOilsPriceTableViewCell"];
    
    @weakify(self)
    [[self.storeImageButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        self.picker = [[LBSingleImagePicker alloc] init];
        [self.picker pickImageInTargetController:self completionHandler:^(UIImage *image) {
            @strongify(self);
            
            // TODO: 此处调用接口上传图片,上传成功后调用接口设置图片
            [OSSImageUploader asyncUploadImage:image complete:^(NSArray<NSString *> *names, UploadImageState state) {
                NSString *imageUrl = [NSString stringWithFormat:@"https://dahuaxiyou-web.oss-cn-beijing.aliyuncs.com/%@", names.firstObject];
                NSDictionary *dict = @{@"companyId": STR(ZYUserInfoModel.sharedInfoModel.userModel.companyId),
                                       @"picture": imageUrl};
                [[DataManager manager] postAddCompanyPictureParam:dict block:^(XRCommonModel *model, NSError *error) {
                    if (model.isSuccess) {
                        [ZYHud showSuccessText:@"公司图片上传成功"];
                        [self.storeImageButton setImage:image forState:UIControlStateNormal];
                        [self.storeImageButton setTitle:@"" forState:UIControlStateNormal];
                    } else {
                        if (![LBLimitInput isEmpty:model.error]) {
                            [ZYHud showErrorText:model.error];
                        }
                    }
                }];
            }];
            
            
        }];
        
    }];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

- (void)configData {
    [ZYHud showLoadingText:@""];
    [[DataManager manager] postFindCompanyInfoParam:@{@"companyId": ZYUserInfoModel.sharedInfoModel.userModel.companyId} block:^(XRCommonModel *model, NSError *error) {
        [ZYHud hideHud];
        if (model.isSuccess) {
            self.storeModel = [ZYMyStoreModel mj_objectWithKeyValues:model.data];
            self.locationLabel.text = self.storeModel.companyInfoResponse.companyAddress;
            self.storeNameLabel.text = self.storeModel.companyInfoResponse.companyName;
            [self.storeImageButton sd_setImageWithURL:[NSURL URLWithString:self.storeModel.companyInfoResponse.companyPicture] forState:UIControlStateNormal];
            
            [self.headerView layoutIfNeeded];
            self.headerView.height = CGRectGetMaxY(self.locationLabel.frame) + 20;
            self.tableView.tableHeaderView = self.headerView;
            [self.tableView reloadData];
        } else {
            if (![LBLimitInput isEmpty:model.error]) {
                [ZYHud showErrorText:model.error];
            }
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.storeModel.oilList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYMyStoreOilsPriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYMyStoreOilsPriceTableViewCell"];
    ZYOilsInfoModel *model = self.storeModel.oilList[indexPath.row];
    cell.model = model;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    UILabel *titleLabel = [UILabel new];
    [sectionView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(sectionView);
        make.left.mas_equalTo(13);
    }];
    titleLabel.text = @"今日油价";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYChangeOilsPriceViewController *controller = [ZYChangeOilsPriceViewController new];
    controller.title = @"修改油品";
    controller.model = self.storeModel.oilList[indexPath.row];
    controller.state = OilsStateChange;
    @weakify(self);
    [controller.subject subscribeNext:^(NSString *text) {
        @strongify(self);
        if ([text isEqualToString:@"change"]) {
            [self.tableView reloadData];
        }
    }];
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - Observer

#pragma mark - Notification

#pragma mark - Event Response

- (IBAction)backButtonClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Override Methods

#pragma mark - Delegate

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Setter / Getter

- (ZYMyStoreModel *)storeModel {
    if (!_storeModel) {
        _storeModel = [[ZYMyStoreModel alloc] init];
    }
    return _storeModel;
}



#pragma mark - Network




@end
